package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IEditPersonMessageModel;
import com.wgts.wgts.mvp.model.ISendSMSCodeModel;
import com.wgts.wgts.mvp.presenter.IBindNewPhoneActivityPresenter;
import com.wgts.wgts.mvp.view.IBindNewPhoneActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.EditMyCenterMessageRequest;
import com.wgts.wgts.netmodle.req.SmsCodeRequest;
import com.wgts.wgts.netmodle.resp.CurrentEntity;
import com.wgts.wgts.netmodle.resp.EditMyCenterMessageEntity;
import com.wgts.wgts.rxjava.MVPResultSubscriber;

/*
 *
 *
 *   data:2018/9/3015:17
 *
 */public class BindNewPhoneActivityPresenter implements IBindNewPhoneActivityPresenter {
     private IBindNewPhoneActivityView view;
     private IEditPersonMessageModel model;
     private ISendSMSCodeModel smsCodeModel;

    public BindNewPhoneActivityPresenter(IBindNewPhoneActivityView view, IEditPersonMessageModel model, ISendSMSCodeModel smsCodeModel) {
        this.view = view;
        this.model = model;
        this.smsCodeModel = smsCodeModel;
    }

    @Override
    public void BindNewPhone(String type, String phone, String code) {
        EditMyCenterMessageRequest request = new EditMyCenterMessageRequest();
        request.updateType = type;
        request.newUserPhone = phone;
        request.newUserPhoneCode = code;
        model.editMyCenterMessageResult(request, new HttpResultSubscriber<EditMyCenterMessageEntity>() {
            @Override
            public void onSuccess(EditMyCenterMessageEntity entity) {
                view.BindNewPhone(entity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void sendSms(String phone, String scene) {
        SmsCodeRequest smsCodeRequest = new SmsCodeRequest();
        smsCodeRequest.scene = scene;
        smsCodeRequest.phone = phone;
        smsCodeModel.sendSMS(smsCodeRequest, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                view.sendSmsResult();
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void addCountDownTimeListener() {
        smsCodeModel.setCutDownTime(new MVPResultSubscriber<Integer>() {
            @Override
            public void netx(Integer integer) {
                if(integer == 0){
                    view.countDownTimeOver();
                    return;
                }
                view.countDowmTime(integer);
            }
        });
    }

    @Override
    public void removeCountDownTimeListener() {
        smsCodeModel.removeCutDownTime();
    }
}
